package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.dao.MemberDao;
import com.itheima.dao.OrderDao;
import com.itheima.dao.OrderSettingDao;
import com.itheima.entity.OrderInfo;
import com.itheima.entity.Result;
import com.itheima.pojo.Member;
import com.itheima.pojo.Order;
import com.itheima.pojo.OrderSetting;
import com.itheima.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

@Service(version = "1.0.0", interfaceClass = OrderService.class)
public class OrderServiceImpl implements OrderService {
    @Autowired
    private OrderSettingDao orderSettingDao;
    @Autowired
    private MemberDao memberDao;
    @Autowired
    private OrderDao orderDao;

    @Override
    public Result save(OrderInfo orderInfo) {

        String date = new SimpleDateFormat("yyyy-MM-dd").format(orderInfo.getOrderDate());
        Order newOrder = new Order();

        OrderSetting orderSetting = orderSettingDao.findDate(date);
        if (orderSetting == null) {
            return Result.fail(-999, "当前日期尚未开放预约");
        }
        int number = orderSetting.getNumber();
        int reservations = orderSetting.getReservations();
        if (reservations >= number) {
            return Result.fail(-999, "当前日期预约已满，请重新预约");
        }
        Member member = memberDao.findByTelephone(orderInfo.getTelephone());

        if (member == null) {
            Member newMember = new Member();
            newMember.setIdCard(orderInfo.getIdCard());
            newMember.setName(orderInfo.getName());
            newMember.setPhoneNumber(orderInfo.getTelephone());
            newMember.setSex(Integer.parseInt(orderInfo.getSex().toString()));
            newMember.setRegTime(new Date());
            memberDao.save(newMember);
            newOrder.setMemberId(newMember.getId());
            newOrder.setOrderDate(orderInfo.getOrderDate());
            newOrder.setOrderStatus("未到诊");
            newOrder.setOrderType("微信预约");
            newOrder.setSetmealId(orderInfo.getSetmealId());
            orderDao.save(newOrder);
            orderSettingDao.updatereservations(date);
        } else {
            Order order = orderDao.findByMemberIdAndDateAndSetMealId(member.getId(), orderInfo.getOrderDate(), orderInfo.getSetmealId());
            if (order != null) {
                return Result.fail(-999, "您已经成功预约一次，系统不支持重复预约");
            } else {
                newOrder.setMemberId(member.getId());
                newOrder.setOrderDate(orderInfo.getOrderDate());
                newOrder.setOrderStatus("未到诊");
                newOrder.setOrderType("微信预约");
                newOrder.setSetmealId(orderInfo.getSetmealId());
                orderDao.save(newOrder);
                orderSettingDao.updatereservations(date);
            }

        }
        System.out.println(newOrder.getId());

        return Result.success(newOrder.getId());
    }

    @Override
    public Result findById(Integer id) {
        Map map = orderDao.findById(id);
        if (map != null) {
            Date orderDate = (Date) map.get("orderDate");
            String date = new SimpleDateFormat("yyyy-MM-dd").format(orderDate);
            map.put("orderDate",date);
        }
        return Result.success(map);
    }

}
